package com.zhang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.entity.ShoppingSysAdmin;
import com.zhang.service.ShoppingSysAdminService;
import com.zhang.units.Result;
import com.zhang.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/shopping-sys-admin")
public class ShoppingSysAdminController {

    @Autowired
    private ShoppingSysAdminService shoppingSysAdminService;
    @Autowired
    private ShoppingSysAdmin shoppingSysAdmin;

    /**
     * 查询管理员列表
     */
    @GetMapping("/adminList")
    public Result queryadmin(@RequestParam("name") String name,
                             @RequestParam("pageNo")int pageNo,
                             @RequestParam("pageSize")int pageSize){
        Page<ShoppingSysAdmin> objectPage = new Page<>(pageNo,pageSize);
        QueryWrapper<ShoppingSysAdmin> wrapper = new QueryWrapper<>();
        wrapper.like("admin_name",name);
        return Result.result200(shoppingSysAdminService.page(objectPage,wrapper));

    }

    /**
     * 新增管理员
     */
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody AdminVo adminVo){
        shoppingSysAdminService.addAdmin(adminVo,shoppingSysAdmin);
        return Result.result200(shoppingSysAdminService.save(shoppingSysAdmin));
    }

    /**
     * 删除（逻辑删除）管理员
     */
    @GetMapping("/deleteLogicAdmin")
    public  Result deleteAdmin(@RequestParam("id")Integer id){
          return Result.result200(shoppingSysAdminService.removeById(id));
    }
    /**
     * 批量删除（逻辑删除）管理员
     */
    @GetMapping("/deleteLogicAdmins")
    public  Result deleteAdmins(@RequestParam("ids")List<Integer> ids){
        return Result.result200(shoppingSysAdminService.removeByIds(ids));
    }

    /**
     * 修改管理员信息
     */
    @PostMapping("/updateAdmin")
    public  Result updateAdmin(@RequestBody AdminVo adminVo){
            shoppingSysAdminService.updateAdmin(adminVo);
        return Result.result200();
    }

    /**
     * 修改管理员密码
     */
    @GetMapping("/updatePassword")
    public Result updatePassword(@RequestParam("password") String password,
                                 @RequestParam("oldPassword1") String oldPassword1,
                                 @RequestParam("oldPassword2") String oldPassword2){
           shoppingSysAdminService.updatePassword(password,oldPassword1,oldPassword2);
            return  Result.result200();
    }


}

